package com.ruoyi.web;

import com.ruoyi.act.config.annotation.VerifyIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.*;

@Component
public class Test implements ApplicationRunner {

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("-------?>"+urls());
    }

    public List<Map<String,Object>> urls() {
        List<Map<String,Object>> urls = new ArrayList<>();
        Map<String,Object> map = null;
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);

        Map<RequestMappingInfo, HandlerMethod> mappingHandlerMethods = mapping.getHandlerMethods();
        for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : mappingHandlerMethods.entrySet()) {
            HandlerMethod handlerMethod = entry.getValue();
            VerifyIgnore verifyIgnore = handlerMethod.getMethodAnnotation(VerifyIgnore.class);
            if (null!=verifyIgnore){
                // 获取url的Set集合，一个方法可能对应多个url
                List<RequestMethod> methods = new ArrayList<>(entry.getKey().getMethodsCondition().getMethods());
                Set<String> patterns = entry.getKey().getPatternsCondition().getPatterns();
                if (methods.size() > 0 && patterns.size() > 0) {
                    map = new HashMap<>();
                    map.put("metohd",methods.get(0));
                    map.put("path",patterns);
                    urls.add(map);
                }
            }

        }
        return urls;
    }
}
